package com.ltmall.api.util;

import javax.servlet.http.HttpServletRequest;

/**
 * 分页对象
 * 调用分页对象，必须在REQUEST中设置qry_cur_page参数
 *
 * @author Administrator
 */
public class PageUtil {

    /**
     * 记录总数
     */
    private int all_rows = 0;

    /**
     * 当前页
     */
    private int cur_page = 1;

    /**
     * 下一页
     */
    private int next_page = 1;

    /**
     * 上一页
     */
    private int prev_page;

    /**
     * 最后一页
     */
    private int last_page;

    /**
     * 所有页
     */
    private int all_page;

    /**
     * 每页记录数
     */
    private int page_row = 20;

    protected PageUtil() {
    }

    public int getNumPerPage() {
        return page_row;
    }

    public void setNumPerPage(int page_row) {
        this.page_row = page_row;
    }

    public int getAll_rows() {
        return all_rows;
    }

    public void setAll_rows(int all_rows) {
        this.all_rows = all_rows;
        //计算页数
        this.all_page = (this.all_rows % this.page_row != 0) ? (this.all_rows / this.page_row + 1) : (this.all_rows / this.page_row);
        //计算下一页
        this.next_page = (this.cur_page + 1 <= this.all_page) ? (this.cur_page + 1) : this.cur_page;
        //计算上一页
        this.prev_page = (this.cur_page - 1 >= 1) ? (this.cur_page - 1) : 1;
        //计算最后一页
        this.last_page = (this.all_page >= 1) ? this.all_page : 1;

    }

    public int getCur_page() {
        return cur_page;
    }

    public void setCur_page(int cur_page) {
        this.cur_page = cur_page;
    }

    public int getNext_page() {
        return next_page;
    }

    public void setNext_page(int next_page) {
        this.next_page = next_page;
    }

    public int getPrev_page() {
        return prev_page;
    }

    public void setPrev_page(int prev_page) {
        this.prev_page = prev_page;
    }

    public int getLast_page() {
        return last_page;
    }

    public void setLast_page(int last_page) {
        this.last_page = last_page;
    }

    public int getAll_page() {
        return all_page;
    }

    public void setAll_page(int all_page) {
        this.all_page = all_page;
    }

    /**
     * 将HttpServletRequest封装为一个分页对象
     *
     * @param request request
     * @return
     */
    public PageUtil(HttpServletRequest request) {
        String cur_page = request.getParameter("qry_cur_page");
        cur_page = TextUtil.nvl(cur_page, "1");
        int c_page = Integer.parseInt(cur_page);
        this.setCur_page(c_page);
    }

    /**
     * 当前页
     *
     * @param c_page
     * @return
     */
    public PageUtil(int c_page) {
        this.setCur_page(c_page);
    }

    public String toHtml() {
        StringBuffer html = new StringBuffer();
        html.append("<div class='page_div'>\n");
        html.append("<span class='page_left'>共" + this.getAll_rows() + "条/" + this.getAll_page() + "页 每页" + this.getNumPerPage() + "条 </span>\n");
        html.append("<span class='page_first' style='cursor:hand;' onclick=\"goPage(1)\" title='第一页'>&nbsp;</span>\n");
        html.append("<span class='page_up'  style='cursor:hand;'   onclick=\"goPage(" + this.getPrev_page() + ")\" title='上一页'>&nbsp;</span>\n");
        html.append("<span class='page_down' style='cursor:hand;'  onclick=\"goPage(" + this.getNext_page() + ")\" title='下一页'>&nbsp;</span>\n");
        html.append("<span class='page_last' style='cursor:hand;'  onclick=\"goPage(" + this.getLast_page() + ")\" title='最后一页'>&nbsp;</span>\n");
        html.append("<span class='page_right'>&nbsp;<a  herf='goPage()' style='cursor:hand;'>转到</u><input onblur='validPage(" + this.getAll_page() + ",this)' name='qry_cur_page' id='qry_cur_page' type='text' value='" + this.getCur_page() + "' style='width:30px;height:16px;'>页</span>\n");
        html.append("</div>\n");
        return html.toString();
    }
}
